<template>
  <div class="bookself">
    <div class="list">
      <dl
        @click="toPage('reader', item)"
        v-for="item of list"
        :key="item.bookId"
      >
        <dt class="cover">
          <img
            class="coverImg"
            :src="item.coverUrl"
            v-default-img="defaultImg"
          />
          <svg-icon
            v-show="item.isUpdate"
            class="update-icon"
            icon-class="update-icon"
          ></svg-icon>
        </dt>
        <dd class="desc">
          <h4 class="bookName ellipsis-two" v-text="item.bookTitle"></h4>
          <p
            class="reading-history"
            :class="{ showReadChapter: item.isRead }"
            :readChapter-content="item.readChapter"
            v-text="item.isRead ? 'Read' : 'Unread'"
          ></p>
        </dd>
      </dl>
    </div>
    <div v-show="isEmpty" class="empty">
      <svg-icon class="empty-icon" icon-class="empty-bookself"></svg-icon>
      <p>
        There is no record yet.
        <span class="toPage" @click="toPage('home')">Go and add books</span>
      </p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      defaultImg: require("@/assets/images/default-cover.png"),
      list: [], //书架列表
      isEmpty: false, //书架是否为空
    };
  },
  components: {},
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.myBookshelf();
  },
  methods: {
    // 获取我的书架
    async myBookshelf() {
      let res = await this.$api.reader.myBookshelf({});
      if (res.code === 1) {
        this.list = res.source;
        if (res.source.length == 0) {
          this.isEmpty = true;
        }
      }
    },
    // 跳转阅读器
    toPage(name, item) {
      if (name === "reader") {
        const { href } = this.$router.resolve({
          name: "reader",
          params: { id: item.bookId }, //书籍id
        });
        window.open(href, "_blank");
      } else if (name === "home") {
        this.$router.push({ name: "home" });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.bookself {
  > .list {
    padding: 20px 0;
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    > dl {
      flex: 0 0 20%;
      margin: 13px 0;
      margin-right: calc(20% / 3);
      cursor: pointer;
      &:nth-child(4n) {
        margin-right: 0;
      }
      > dt.cover {
        position: relative;
        .coverImg {
          width: 100%;
          height: 220px;
          border-radius: 16px;
          box-shadow: 0px 1px 6px 1px rgba(0, 0, 0, 0.2);
        }
        .update-icon {
          width: 90px;
          height: 32px;
          position: absolute;
          top: 0;
          left: 0;
        }
      }
      > dd.desc {
        > .bookName {
          height: 40px;
          margin-top: 16px;
          font-size: 14px;
          font-weight: 500;
          color: #000000;
          line-height: 20px;
          font-family: Poppins-Medium, Poppins;
        }
        > .reading-history {
          font-size: 14px;
          font-family: Poppins-Regular, Poppins;
          font-weight: 400;
          color: #999999;
          line-height: 23px;
          &.showReadChapter::after {
            content: attr(readChapter-content);
            margin-left: 8px;
          }
        }
      }
    }
  }
  > .empty {
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .empty-icon {
      width: 276px;
      height: 195px;
    }
    > p {
      margin-top: 41px;
      font-size: 14px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 500;
      color: #999999;
      line-height: 20px;
      .toPage {
        cursor: pointer;
        text-decoration: underline;
        color: #e86472;
      }
    }
  }
}
</style>
